package com.yunfeisoft.business.service.impl;

import com.yunfeisoft.business.dao.inter.ElderPharmacyDao;
import com.yunfeisoft.business.model.ElderPharmacy;
import com.yunfeisoft.business.service.inter.ElderPharmacyService;
import com.yunfeisoft.common.base.BaseServiceImpl;
import com.yunfeisoft.common.sql.separation.DataSourceChange;
import com.yunfeisoft.common.utils.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * ClassName: ElderPharmacyServiceImpl
 * Description: 主要用药情况从表service实现
 * Author: Jackie liu
 * Date: 2020-04-04
 */
@Service("elderPharmacyService")
public class ElderPharmacyServiceImpl extends BaseServiceImpl<ElderPharmacy, String, ElderPharmacyDao> implements ElderPharmacyService {

    @Override
    @DataSourceChange(slave = true)
    public Page<ElderPharmacy> queryPage(Map<String, Object> params) {
        return getDao().queryPage(params);
    }

    @Override
    public List<ElderPharmacy> getByHealthId(String elderHealthId) {
        return getDao().getByHealthId(elderHealthId);
    }

    @Override
    public void deleteByHealthId(String elderHealthId) {
        getDao().deleteByHealthId(elderHealthId);
    }

    @Transactional
    public void saveElderPharmacy(List<ElderPharmacy> elderPharmacyList) {
        if (!ObjectUtils.isEmpty(elderPharmacyList)) {
            // 先删除之前的记录
            getDao().deleteByHealthId(elderPharmacyList.get(0).getElderHealthId());
            // 添加记录
            for (ElderPharmacy elderPharmacy : elderPharmacyList) {
                getDao().insert(elderPharmacy);
            }
        }
    }
}